var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var MPage = require('../MPage.js');
var common = require('../../utils/common.js');
var Pagination = require('../../utils/pagination.js');
var app = getApp();

var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置

Page(common.extendPage(MPage, {
    data: {
        dataLoaded: false,
        list: [],

        tabs: ["全部", "收入", "支出"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        sliderWidth,

        page1: {
            nextPageLoading: false,
            pageLoadedAll: false,
        },
        page2: {
            nextPageLoading: false,
            pageLoadedAll: false,
        },
        page3: {
            nextPageLoading: false,
            pageLoadedAll: false,
        },
    },

    onLoad: function() {
        this._initTabs();
        this._getScoreList(0);
        this._getScoreList(1);
        this._getScoreList(2);
    },

    onReachBottom() {
        var _self = this;
        var num = +_self.data.activeIndex + 1;
        var pagination;

        if (_self.data['page' + num].pageLoadedAll) {
            return;
        }

        pagination = _self['pagination' + num];

        pagination && pagination.loadNext();
    },

    _initTabs() {
        var that = this;

        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
                });
            }
        });
    },

    tabClick: function(e) {
        var id = e.currentTarget.id;
        var formId = e.detail.formId;

        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: id
        });
        common.submitForm(formId);
    },

    //已收藏商品列表
    _getScoreList(type) {
        var _self = this;
        var pageOrder = +type + 1;

        _self['pagination' + pageOrder] = new Pagination({
            url: '/openapi/CUser/jifenList',
            params: {
                type,
            },
            beforeGetPage(page) {
                var pageObj = _self.data['page' + pageOrder];

                if (page > 1) {
                    pageObj.nextPageLoading = true;
                    _self.setData({
                        [`page${pageOrder}`]: pageObj
                    });
                }
            },
            getPageSuccess(page, data) {
                var result = data.result;
                var pageObj = _self.data['page' + pageOrder];
                var list = _self.data['list' + pageOrder] || [];
                var curList = result['bill'] || [];
                var data = {};

                if (page == 1) {
                    if (pageOrder == 1) {
                        data = {
                            dataLoaded: true,
                        };
                    }
                    list = curList;
                } else {
                    list = list.concat(curList)
                }

                if (curList.length < 10) {
                    pageObj.pageLoadedAll = true;
                }

                pageObj.nextPageLoading = false;

                data['page' + pageOrder] = pageObj;
                data['list' + pageOrder] = list;
                data['score'] = result['jifen'];
                data['isSign'] = result['qiandao'];

                _self.setData(data);
            }
        });
    },

    //签到
    _sign() {
        var _self = this;

        ajax.P('/openapi/CUser/perdayQiandao', {}, function(data) {
            var addScore = data.result.jifen;

            util.alert('签到成功, 积分+' + addScore, function() {
                var score = _self.data.score;

                _self.setData({
                    score: score + addScore,
                    isSign: true
                });
                _self.pagination1.flush();
                _self.pagination2.flush();
                _self.pagination3.flush();
            });
        });
    }

}));